package com.test.demo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;

public class DatabaseHelper extends SQLiteOpenHelper {
 	
	public DatabaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		
		super(context, name, factory, version);
 		System.out.println("new DatabaseHelper: "+version);//add for test
		// TODO Auto-generated constructor stub
 	}
	
	public DatabaseHelper(Context context, String name, int version){   
		this(context,name,null,version);   
	}   

	public DatabaseHelper(Context context, String name){   
		this(context,name,DatabaseManager.DB_VERSION);   
	}   


	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		System.out.println("onCreate: SQLiteDatabase");//add for test
		
		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_DEVICE_INFO+" (id integer primary key AUTOINCREMENT," +
				"dev_id integer," +
				"name text," +
				"ip text," +
				"port integer," +
				"username text," +
				"password text," +
				"domain text," +
				"save_type integer,"+
				"new_msg_count integer," +//add by luo 20141015
				"last_fresh_time long,"+//add by luo 20141015
				"last_get_time long,"+//add by luo 20141015
				"online_stat integer," +//add by luo 20150711
				"online_stat_time long,"+//add by luo 20150711
				"face BLOB)");  
 
		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_REC_FILE+" (id integer primary key AUTOINCREMENT," +
				"file_id integer," +
				"file_size integer," +
				"name text," +
				"start_hour integer," +
				"start_min integer," +
				"start_sec integer," +
				"file_time_len integer)");
		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_MR_SERVER+" (id integer primary key AUTOINCREMENT," +
				"server_id integer," +
				"is_init integer," +
				"init_time integer," +
				"domain text," +
				"ip text)");
		
		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_SERVER_FACE+" (id integer primary key AUTOINCREMENT," +
				"server_id integer," +
				"data_size integer," +
				"face blob)");
		

		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_ALARM_MESSAGE+" (id integer primary key AUTOINCREMENT," +
				"save_id int,"+
				"alarm_id int," +
				"dev_id int," +
				"alarm_type int," +
				"alarm_level int," +
				"alarm_msg text," +
				"alarm_time text," +
				"save_time long,"+
				"image blob)"); 
		
		db.execSQL("CREATE TABLE IF NOT EXISTS "+DatabaseManager.TABLE_PTZX_FACE+" (id integer primary key AUTOINCREMENT," +
				"dev_id int," +
				"ptzx_id int,"+
				"save_time long,"+
				"image blob)"); 
		
		
	}

 
 
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		System.out.println("onUpgrade: "+oldVersion+""+newVersion);//add for test
		// TODO Auto-generated method stub
		if(oldVersion<newVersion){
			try{
				String sql = " drop table " + DatabaseManager.TABLE_DEVICE_INFO;
				db.execSQL(sql);
				
				sql = " drop table " + DatabaseManager.TABLE_REC_FILE;
				db.execSQL(sql);
				
				sql = " drop table " + DatabaseManager.TABLE_MR_SERVER;
				db.execSQL(sql);
				
				sql = " drop table " + DatabaseManager.TABLE_ALARM_MESSAGE;
				db.execSQL(sql);
				
				sql = " drop table " + DatabaseManager.TABLE_PTZX_FACE;
				db.execSQL(sql);
			}catch(Exception e){
				
			}
			
			
			onCreate(db);
			
		}
	}
	
	  @Override
	  public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		  if(newVersion<oldVersion){
				try{
					String sql = " drop table " + DatabaseManager.TABLE_DEVICE_INFO;
					db.execSQL(sql);
					
					sql = " drop table " + DatabaseManager.TABLE_REC_FILE;
					db.execSQL(sql);
					
					sql = " drop table " + DatabaseManager.TABLE_MR_SERVER;
					db.execSQL(sql);
					
					sql = " drop table " + DatabaseManager.TABLE_ALARM_MESSAGE;
					db.execSQL(sql);
					
					sql = " drop table " + DatabaseManager.TABLE_PTZX_FACE;
					db.execSQL(sql);
				}catch(Exception e){
					
				}
				onCreate(db);
				
			}
	  }
}
